package com.edol.painting.widget;

import android.content.Context;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.edol.painting.R;

public class BackgroundPaperContainer extends LinearLayout {

    private static final String TAG = "BackgroundPaperContainer";

    public static final int[] EDIT_BACK_PAGE = {R.drawable.paper_back_one,
		R.drawable.paper_back_two, R.drawable.paper_back_three,
		R.drawable.paper_back_four, R.drawable.paper_back_five,
		R.drawable.paper_back_six, R.drawable.paper_back_seven,
		R.drawable.paper_back_eight, R.drawable.paper_back_nine,
		R.drawable.paper_back_ten, R.drawable.paper_back_eleven,
		R.drawable.paper_back_twelve, R.drawable.paper_back_thirteen,
		R.drawable.paper_back_fourteen, R.drawable.paper_back_fifteen,
		R.drawable.paper_back_sixteen, R.drawable.select_background_color};

    public static final int EDIT_BACK_PAPER_COUNT = EDIT_BACK_PAGE.length;
    public static final int EDIT_BACK_PAPER_IDX = EDIT_BACK_PAGE.length - 1;

    private int mChildCount = EDIT_BACK_PAPER_COUNT;
    
    private OnItemClickListener mItemClickListener;
    private OnSizeChangeListener mSizeChangeListener;

    private int mSelectedIndex = 0;

    private int mHeight = 250;
    private int mWidth = 1830;
    
    private ColorDrawable mChangeDefaultBgColor;

    private int mChildSize = 220;
    private int mChildPadding;
    private int mChildSelect;
    
    public BackgroundPaperContainer(Context context) {
        super(context);
    }

    public BackgroundPaperContainer(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
    }

    public BackgroundPaperContainer(Context context, AttributeSet attrs) {
        super(context, attrs);
        loadShader();
    }

    public void addChilds(int childCount, int color) {
        mChildCount = childCount;
        for (int i = 0; i < mChildCount; i++) {
            addView(productChild(mChildSize, mChildSize, i, color));
        }
        mWidth = mChildCount * mChildSize + getPaddingLeft() + getPaddingRight();
        mHeight = mChildSize + getPaddingTop() + getPaddingBottom();
    }
    
    private void loadShader() {
    	mChildSize = getResources().getDimensionPixelSize(R.dimen.bg_paper_child_size);
    	mChildPadding  = getResources().getDimensionPixelSize(R.dimen.bg_paper_child_padding);
    	mChildSelect = getResources().getDimensionPixelSize(R.dimen.bg_paper_child_select);
    }

    public void setDefaultColor(int color) {
    	mChangeDefaultBgColor.setColor(color);
    }
    
    public ColorDrawable getDefaultColor() {
    	return mChangeDefaultBgColor;
    }

    private View productChild(int width, int height, int index, int color) {
        View view = new View(getContext());
        view.setId(index);
        view.setClickable(true);
        view.setLayoutParams(new LayoutParams(width, height));
        Drawable[] layers;
        if (index != EDIT_BACK_PAPER_IDX) {
        	layers = new Drawable[2];
        	layers[1] = getResources().getDrawable(R.drawable.paper_item_bg_selected);
        	layers[0] = (BitmapDrawable)getResources().getDrawable(EDIT_BACK_PAGE[index]);
        	((BitmapDrawable)layers[0]).setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
        } else {
        	layers = new Drawable[3];
        	layers[1] = getResources().getDrawable(R.drawable.paper_item_bg_selected);
        	layers[2] = (BitmapDrawable)getResources().getDrawable(EDIT_BACK_PAGE[index]);
        	layers[0] = new ColorDrawable(color);
        	mChangeDefaultBgColor = (ColorDrawable)layers[0];
        }
        
        LayerDrawable layerDrawable = new LayerDrawable(layers);
        layerDrawable.setLayerInset(0, mChildPadding, mChildPadding, mChildPadding, mChildPadding);
        layerDrawable.setLayerInset(1, mChildSelect, mChildSelect, mChildSelect, mChildSelect);
        if (index != mSelectedIndex) {
            layers[1].setAlpha(0);
        }
        view.setBackground(layerDrawable);
        view.setOnClickListener(mListener);
        return view;
    }

    private OnClickListener mListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (mSelectedIndex != id) {
                LayerDrawable bg = (LayerDrawable)v.getBackground();
                bg.getDrawable(1).setAlpha(255);

                LayerDrawable old = (LayerDrawable)getChildAt(mSelectedIndex).getBackground();
                old.getDrawable(1).setAlpha(0);

                mSelectedIndex = id;
            }
            if (mItemClickListener != null) {
            	mItemClickListener.onItemClick(v);
            }	
        }
    };

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
    }

    private int measureHeight(int heightMeasureSpec) {
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        if (heightSpecMode == MeasureSpec.EXACTLY) {
            return MeasureSpec.getSize(heightMeasureSpec);
        } else {
            return Math.min(mHeight, MeasureSpec.getSize(heightMeasureSpec));
        }
    }

    private int measureWidth(int widthMeasureSpec) {
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        if (widthSpecMode == MeasureSpec.EXACTLY) {
            return MeasureSpec.getSize(widthMeasureSpec);
        } else {
            return mWidth;
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    	super.onSizeChanged(w, h, oldw, oldh);
    	if (mSizeChangeListener != null) {
    		mSizeChangeListener.onSizeChange(w, h);
    	}
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }

    public interface OnItemClickListener {
        void onItemClick(View view);
    }
    
    public void setOnItemClickListener(OnItemClickListener listener) {
        mItemClickListener = listener;
    }
    
    public interface OnSizeChangeListener {
    	void onSizeChange(int w, int h);
    }
    
    public void setOnSizeChangeListener(OnSizeChangeListener listener) {
    	mSizeChangeListener = listener;
    }
    
    public void clear() {
    	mChangeDefaultBgColor.setCallback(null);
    	for (int i = 0; i < mChildCount; i ++) {
    		LayerDrawable ld = (LayerDrawable)getChildAt(mSelectedIndex).getBackground();
    		ld.setCallback(null);
    	}
    }

}
